import Vue from "vue";
import VueRouter from "vue-router";
import Home from "../views/Home.vue";

import { isLogined } from "../utils/tools";

Vue.use(VueRouter);

const routes = [
	{
		path: "/",
		name: "Home",
		component: Home,
	},
	{
		path: "/list",
		name: "List",
		// route level code-splitting
		// this generates a separate chunk (about.[hash].js) for this route
		// which is lazy-loaded when the route is visited.
		component: () =>
			import(/* webpackChunkName: "about" */ "../views/List.vue"),
	},
	{
		path: "/cart",
		name: "Cart",
		component: () => import("../views/Cart.vue"),
		meta: {
			needLogin: true,
		},
	},
	// {
	// 	path: "/community",
	// 	name: "Community",
	// 	component: () => import("../views/Community.vue"),
	// },
	{
		path: "/detail",
		name: "Detail",
		component: () => import("../views/Detail.vue"),
		meta: {
			hideNav: true, //隐藏底部导航
		},
	},
	{
		path: "/order",
		name: "Order",
		component: () => import("../views/Order.vue"),
	},
	{
		path: "/login",
		name: "Login",
		component: () => import("../views/Login.vue"),
		meta: {},
	},
	{
		path: "/register",
		name: "Register",
		component: () => import("../views/Register.vue"),
	},
	{
		path: "/departs",
		name: "Departs",
		component: () => import("../views/Departs.vue"),
	},
	{
		path: "/user",
		name: "User",
		component: () => import("../views/User.vue"),
		meta: {
			needLogin: true,
		},
	},
	{
		path: "/address",
		name: "Address",
		component: () => import("../views/Address.vue"),
	},
	{
		path: "/personal",
		name: "Personal",
		component: () => import("../views/Personal.vue"),
	},
  {
		path: "/change",
		name: "Change",
		component: () => import("../views/Change.vue"),
	},
];

const router = new VueRouter({
	mode: "history",
	base: process.env.BASE_URL,
	routes,
});

//登录判断
router.beforeEach((to, from, next) => {
	if (to.meta.needLogin) {
		if (isLogined()) {
			next();
		} else {
			next({
				name: "Login",
			});
		}
	} else {
		next();
	}
});

export default router;
